<?php
/*
 * @Author: your name
 * @Date: 2020-06-05 17:32:41
 * @LastEditTime: 2020-10-27 13:48:57
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /special/application/api/service/Config.php
 */

namespace app\api\service;


use app\common\exception\CustomException;
use app\common\file_path\NetfilterFilePath;
use app\common\model\FilePathModel;
use think\Db;

class Config
{
    /**
     * @name 下发syslog配置文件
     * @author wx
     * @date 2020/1/8 14:08
     */
    public function syslog()
    {
        $syslogPath = '../runtime/config/syslog.conf';

        if (!file_exists($syslogPath)) {
            throw new CustomException('no syslog config file', 422);
        }

        $content = file_get_contents($syslogPath);
        $syslogConfig = json_decode($content, true);
        return $syslogConfig;

    }

    /**
     * @name 端口设置
     * @param $data
     * @author wx
     * @date 2020/1/8 14:52
     */
    public function portConfig($data)
    {

        if(!empty($data)){
            foreach ($data as $type => $port){
                Db::name('server_proxy')->where('type',$type)->update(['port' => $port]);
            }
        }

        return 'success';
    }

    /**
     * @name 获取服务端配置
     * @author wx
     * @date 2020/12/28 11:14
     */
    public function server()
    {
        $this->mkdirServer();
        $return['file_path'] = $this->getFilePath();
        $return['database'] = $this->getDatabase();

        return $return;
    }

    /**
     * @name 获取服务端路径信息
     * @author wx
     * @date 2020/12/28 11:17
     */
    protected function getFilePath()
    {
        //1. 文件路径
        $filePathModel = new FilePathModel();

        $publicPath = $_SERVER['DOCUMENT_ROOT'].'/';
        $phpPath = dirname($publicPath).'/';
        $runtimePath = $phpPath.'runtime/';

        $ruleDir = NetfilterFilePath::WAITING_FOR_SORT;//规则排序文件目录


        $config['server_pc_info'] = $runtimePath.'config/server_pcinfo.json';//平台资源使用率文件
        $config['crontab'] = $phpPath.'time_exec.php';//定时执行文件
        $config['lock_time'] = $publicPath.$filePathModel->system_time;//系统时间文件
        $config['json_time'] = $publicPath.'Install/system_time.json';//安装时生成的时间文件
        $config['syslog_cfg'] = $runtimePath.'config/syslog.conf';//syslog配置文件
        $config['elk_cfg'] = $runtimePath.'config/elk_config.conf';//elk配置文件
        $config['rule_rank'] = str_replace('../runtime/', $runtimePath, $ruleDir);//规则排序文件目录

        //更新
        $config['update_lock'] = $publicPath.$filePathModel->update_lock;//正在更新标识性文件
        $config['offline_lock'] = $publicPath.$filePathModel->offline_lock;//离线更新文件
        $config['windows_client_update_package_dir'] = $publicPath.$filePathModel->windows_client_update_package_dir;
        $config['linux_client_update_package_dir'] = $publicPath.$filePathModel->linux_client_update_package_dir;
        $config['server_update_package_dir'] = $publicPath.$filePathModel->server_update_package_dir;
        $config['middleware_update_package_dir'] = $publicPath.$filePathModel->middleware_update_package_dir;

        return $config;
    }

    /**
     * @name 创建服务端目录
     * @author wx
     * @date 2020/12/28 11:19
     */
    protected function mkdirServer()
    {
        $filePathModel = new FilePathModel();

        //需要创建的目录列表
        $isMakeDir = [
            $filePathModel->windows_client_update_package_dir,
            $filePathModel->linux_client_update_package_dir,
            $filePathModel->server_update_package_dir,
            $filePathModel->middleware_update_package_dir
        ];

        foreach ($isMakeDir as $v){
            if (!file_exists($v)) {
                mkdir($v, 0777, true);
            }
        }
    }

    /**
     * @name 获取数据库配置
     * @return mixed
     * @author wx
     * @date 2020/12/28 11:21
     */
    protected function getDatabase()
    {
        //2. 数据库连接配置
        $databaseConfig = config('database.');

        $database['ip'] = $databaseConfig['hostname'];
        $database['port'] = (string) $databaseConfig['hostport'];
        $database['user'] = $databaseConfig['username'];
        $database['password'] = $databaseConfig['password'];
        $database['name'] = $databaseConfig['database'];
        $database['prefix'] = $databaseConfig['prefix'];

        return $database;
    }

}